import pygame


def openSnake(num, snakeheadgroup, foodgroup, currentscore):
    # 自动搜索食物
    for head in snakeheadgroup:
        closest_food = None
        closest_distance = float('inf')

        for food in foodgroup:
            food_position = pygame.math.Vector2(food.rect.topleft)
            head_position = pygame.math.Vector2(head.rect.topleft)
            distance = (food_position - head_position).length()

            if distance < closest_distance:
                closest_food = food
                closest_distance = distance

        if closest_food:
            food_position = closest_food.rect.topleft
            head_position = head.rect.topleft

            if food_position[0] < head_position[0]:
                head.movedir = 'left'
            elif food_position[0] > head_position[0]:
                head.movedir = 'right'
            elif food_position[1] < head_position[1]:
                head.movedir = 'up'
            elif food_position[1] > head_position[1]:
                head.movedir = 'down'
        if currentscore >= int(num):
            return True
    return False
